import Vue from 'vue'
import Router from 'vue-router'
import store from "@/store/index"

Vue.use(Router)

const routes = [
  {
    path:'/',
    redirect:'/login',
  },
  {
    path:"/home",
    component:()=>import('@/views/home/index'),
    name:"home"
  },
  {
    path:"/login",
    component:()=>import('@/views/login/index'),
    name:"login"
  }
]

const createRouter = ()=>{
  return new Router({
    scrollBehavior:()=>({y:0}),
    routes
  })
}

const router = createRouter()

export function resetRouter(){
  const newRouter = createRouter()
  router.match = newRouter.matcher
}

export default router

router.beforeEach((to,from,next)=>{

  if(to.path == "/home"){
    if(store.state.userInfo){
      next()
    }else{
      next("/login")
    }
  }else{
    next() 
  }
})